package com.parking.mgr.common.dbs;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.service.IService;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @ClassName BaseService
 * @Description 基础 Service
 * @Auther: zhijianpu
 * @Date: 2023/5/18
 */
public interface BaseService<Entity> extends IService<Entity> {

    /**
     * 查询逻辑删除的记录
     *
     * @param wrapper
     * @return
     */
    default List<Entity> selectLogicDelete(QueryWrapper<Entity> wrapper) {
        if (getBaseMapper() instanceof BaseMapper<Entity> baseMapper) {
            return baseMapper.selectLogicDelete(wrapper);
        }
        return new ArrayList<>();
    }

    /**
     * 恢复逻辑删除的记录
     *
     * @param ids
     * @return
     */

    default int revertLogicDeleted(Collection<? extends Serializable> ids) {
        if (getBaseMapper() instanceof BaseMapper<Entity> baseMapper) {
            return baseMapper.revertLogicDeleted(ids);
        }
        return 0;
    }

    /**
     * 物理删除字段
     *
     * @param ids
     * @return
     */
    default int physicsDelete(Collection<? extends Serializable> ids) {
        if (getBaseMapper() instanceof BaseMapper<Entity> baseMapper) {
            QueryWrapper<Entity> queryWrapper = new QueryWrapper<Entity>().in("id", ids);
            return baseMapper.physicsDelete(queryWrapper);
        }
        return 0;
    }

    /**
     * 物理删除字段
     *
     * @param queryWrapper
     * @return
     */
    default int physicsDelete(QueryWrapper<Entity> queryWrapper) {
        if (getBaseMapper() instanceof BaseMapper<Entity> baseMapper) {
            return baseMapper.physicsDelete(queryWrapper);
        }
        return 0;
    }

    /**
     * 物理删除字段
     *
     * @param queryWrapper
     * @return
     */
    default int physicsDelete(LambdaQueryWrapper<Entity> queryWrapper) {
        if (getBaseMapper() instanceof BaseMapper<Entity> baseMapper) {
            return baseMapper.physicsDelete(queryWrapper);
        }
        return 0;
    }

    /**
     * 批量插入字段
     *
     * @param entities
     * @return
     */
    default int insertBatchSomeColumn(List<Entity> entities) {
        if (getBaseMapper() instanceof BaseMapper<Entity> baseMapper) {
            return baseMapper.insertBatchSomeColumn(entities);
        }
        return 0;
    }

}
